package wfc.dao.daoImpl;


import wfc.dao.DishDao;
import wfc.entity.Dish;
import wfc.util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class DishDaoImpl implements DishDao {
    @Override
    public List<Dish> getDishByTitle() {
        List<Dish> newList=new ArrayList<Dish>();
        ResultSet rs=null;
        try {
            //（3）获得Statement对象，执行SQL语句
            String sql="select id,dname,uname,love,img,category from dish ";
            Object[] params={};
            rs= BaseDao.executeSQL(sql, params);
            //（4）处理执行结果(ResultSet)，
            while(rs.next()){
                int id=rs.getInt("id");
                String daname=rs.getString("dname");
                String uname=rs.getString("uname");
                int love=rs.getInt("love");
                String img=rs.getString("img");
                String category=rs.getString("category");


                Dish dish = new Dish();
                dish.setId(id);
                dish.setDname(daname);
                dish.setUname(uname);
                dish.setLove(love);
                dish.setImg(img);
                dish.setCategory(category);


                newList.add(dish);
            }
        }catch (SQLException e) {

            e.printStackTrace();
        }finally{
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            BaseDao.closeConnection(rs,preparedStatement,connection);
        }
        return newList;
    }

    @Override
    public boolean addDish(Dish dish) {
        boolean flag=false;
        try {
            String sql="insert into dish(dname,uname,love,img,category ) values(?,?,?,?,?)";
            Object[] params={dish};
            int i=BaseDao.dishInsert(sql, params);
            //（4）处理执行结果
            if(i>0){
                System.out.println("新增食品成功");
            }
            flag=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public Dish selectBydname(String dname) {
        List<Dish> newList=new ArrayList<Dish>();
        ResultSet resultSet =null;
        Dish dish = new Dish();
        try {
            String  sql = "select id,dname,uname,love,img,category from dish where dname=? ";
            Object[] obj={dname};
            resultSet = BaseDao.executeSQL(sql, obj);
            while(resultSet.next()) {
                int id=resultSet.getInt("id");
                String daname=resultSet.getString("dname");
                String uname=resultSet.getString("uname");
                int love=resultSet.getInt("love");
                String img=resultSet.getString("img");
                String category=resultSet.getString("category");

                dish.setId(id);
                dish.setDname(daname);
                dish.setUname(uname);
                dish.setLove(love);
                dish.setImg(img);
                dish.setCategory(category);

            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            BaseDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return dish;
    }

    @Override
    public List<Dish> selectByuname(String uname) {
        List<Dish> newList=new ArrayList<Dish>();
        ResultSet resultSet =null;
        Dish dish = null;
        try {
            String  sql = "select id,dname,uname,love,img,category from dish where uname=? ";
            Object[] obj={uname};
            resultSet = BaseDao.executeSQL(sql, obj);
            while(resultSet.next()) {
                dish = new Dish();
                int id=resultSet.getInt("id");
                String daname=resultSet.getString("dname");
                String uname1=resultSet.getString("uname");
                int love=resultSet.getInt("love");
                String img=resultSet.getString("img");
                String category=resultSet.getString("category");

                dish.setId(id);
                dish.setDname(daname);
                dish.setUname(uname1);
                dish.setLove(love);
                dish.setImg(img);
                dish.setCategory(category);
                newList.add(dish);

            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            BaseDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return newList;
    }

    @Override
    public int update(Dish dish) {
        int rows = 0;
        String sql = "UPDATE `dish` SET dname=? ,uname=? ,love=?,img=?,category=? WHERE id=?";
        Object[] obj = {dish};
        rows = BaseDao.dishUpdate(sql, obj);
        return rows;
    }
}
